/*
 * Created on Nov 27, 2005
 */
package dbsucxentW.pathProcessorEdgeW;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

/**
 * Keeps track of steps already processed
 * @author Seah Boon Siew
 */ 
public class CurrentPath {
	String _currentPath = "";
	int _count = 1;
	Connection _dbConnection;
	
	public CurrentPath(Connection conn) { _dbConnection = conn; };
	
	// add step without adding name
	public void addStep(){
		_count++;
	}
	
	public void addString(String string){
		_currentPath += string;
	}
	
	public void add(String step){
		if (step.compareTo("") != 0 ){
			//klarinda 20060824 add node()
			if (step.compareTo("*") == 0 || step.compareTo("node()") == 0){
				step = "%";
			} // klarinda 20060825 else{
			_count++;
			//}
			
			_currentPath += "." + step + "#";
			System.out.println("CurrentPath.add("+step+"), "+_currentPath);			//klarinda 20060824
		}
	}
	public String getPathString(){
		return _currentPath;
	}
	
	// convert path expression into a sequence of path ids
	public String getPath(){
		
		try{
			Statement statement = _dbConnection.createStatement ();
			System.out.println("CurrentPath.getPath(), SELECT path_id FROM PATH WHERE PATH LIKE '" + _currentPath + "%'");
			ResultSet results = statement.executeQuery ("SELECT path_id FROM PATH WHERE PATH LIKE '" + _currentPath + "%'");
			String res = "(";
			boolean first = true;
			while (results.next()){
				if (first){
					res += results.getString("Path_id");
					first = false;
				}
				else	{
					res += "," + results.getString("Path_id");
				}
			}
			
			res += ")";	
			
			results.close();
			statement.close();
			
			return res;
			
		}catch (Exception ex){
    		System.out.println(ex.getMessage());
    		JOptionPane.showMessageDialog(null,ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    		return "ERROR";
    	}
		
				
	}
	
	public String getActualPath(){
		
		try{
			Statement statement = _dbConnection.createStatement ();
			System.out.println("CurrentPath.getActualPath(), SELECT path_id FROM PATH WHERE PATH LIKE '" + _currentPath + "'");
			ResultSet results = statement.executeQuery ("SELECT path_id FROM PATH WHERE PATH LIKE '" + _currentPath + "'");
			String res = "(";
			boolean first = true;
			
			while (results.next()){
				if (first){
					res += results.getString("Path_id");
					first = false;
				}
				else	{
					res += "," + results.getString("Path_id");
				}
			}
			
			res += ")";	
			
			results.close();
			statement.close();
			return res;
			
		}catch (Exception ex){
    		System.out.println(ex.getMessage());
    		JOptionPane.showMessageDialog(null,ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    		return "ERROR";
    	}
				
	}
	
	public int getLevel(){
		return _count;
	}
	
	public void clearPath(){
		_currentPath = "";
	}
	
	//copied from LTM
	public void removeLast() {
        String[] parts = _currentPath.split("\\.");
        _currentPath = new String();
        for (int i = 1; i < parts.length - 1; i++) {
        	_currentPath += ".";
        	_currentPath += parts[i];
        }
        _count--;
	}

}
